package logic

import (
	"context"
	"errors"
	"furni-suite/furnirpc/furnirpc"
	"furni-suite/furnirpc/internal/svc"
	"furni-suite/model"
	"github.com/zeromicro/go-zero/core/logx"
)

type FurniAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFurniAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FurniAddLogic {
	return &FurniAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 添加
func (l *FurniAddLogic) FurniAdd(in *furnirpc.FurniAddRequest) (*furnirpc.FurniAddResponse, error) {
	// todo: add your logic here and delete this line
	var findgoods model.Furni
	if err := findgoods.FindFurniByName(model.DB, in.FurniName); err == nil {
		return nil, errors.New("已存在")
	}
	class := &model.Class{
		ClassName: in.ClassName,
	}
	if err := class.FindClassByName(model.DB, in.ClassName); err != nil {
		return nil, errors.New("创建失败")
	}
	brand := &model.Brand{
		BrandName: in.BrandName,
	}
	if err := brand.FindBrandByName(model.DB, in.BrandName); err != nil {
		return nil, errors.New("创建失败")
	}
	data := model.Furni{
		FurniName:  in.FurniName,
		FurniPrice: float64(in.FurniPrice),
		Bid:        int(brand.ID),
		Cid:        int(class.ID),
	}
	if err := data.FurniAdd(model.DB); err != nil {
		return nil, errors.New("添加失败")
	}
	return &furnirpc.FurniAddResponse{
		FurniId: int64(data.ID),
	}, nil
}
